package com.xcz.demo03;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author XCZ
 */
//自动生成代理类
public class ProxyInvocationHandle implements InvocationHandler {
    private Rent rent;

    public void setRent(Rent rent) {
        this.rent = rent;
    }

    //生成得到代理类
    public Object getProxy() {
        System.out.println("生成代理类");
        // Proxy.newProxyInstance(表示加载的类的位置，表示接口是哪个接口)
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), rent.getClass().getInterfaces(), this);
    }

    //处理代理实例并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理类实例返回");
        seeHouse();
        //invoke反射
        Object result = method.invoke(rent, args);
        charge();
        return result;
    }

    public void seeHouse() {
        System.out.println("中介带看房子");
    }

    public void charge() {
        System.out.println("收钱");
    }
}
